var moduleSysmenu = angular.module('moduleSysmenu', ['paginationService', 'anajaxService','agularValidate','innerPaginationService']);

moduleSysmenu.controller('SysMenuController', function($scope, $http,pagination,anajax,innerPagination) {
	$scope.purForm={};
	$scope.menuForm={};
	$scope.pruviewResultList={};
	$scope.menuResultList={};
	$scope.purQueryForm={};
	$scope.menuQueryForm={};
	$scope.setting = {
			callback: {
				onClick:  zTreeOnClick
			},
			data: {
				key:{
					name:"sname",
					title:"sname"
				},
				simpleData: {
					idKey:"sysMenuId",
					pIdKey:"parentid",
					enable: true
				}
			}
		};
	/**
	 * 查询菜单
	 */
var queryMenu = function(params) {
	var url = commonutil.actionPath + '/sys/menu/menu_list';
	anajax.doajax(url, params, function(data) {
		$scope.menuResultList = data;
		pagination.setData(data.pagination);
		$scope.pagination = data.pagination;
		if(data.length>0){
			$("#newPurview").hide();
		$("#menu_list_table").show();
		}else{
			$("#menu_list_table").hide();
			$("#pruview_list_table").hide();
			$("#newPurview").show();
			$("#newMenu").show();
		}
	});
};


var queryPurview = function(params) {
	var url = commonutil.actionPath + '/sys/menu-purview/list';
	anajax.doajax(url, params, function(data) {
		$scope.pruviewResultList = data.resultList;
		pagination.setData(data.pagination);
		$scope.pagination = data.pagination;
		if(data.resultList.length>0){
		$("#pruview_list_table").show();
		$("#newMenu").hide();
		}else{
			$("#menu_list_table").hide();
			$("#pruview_list_table").hide();
			$("#newPurview").show();
			$("#newMenu").show();
		}
	});
};

	
	//ztree 树形菜单节点点击事件
	function zTreeOnClick(event, treeId, treeNode){
		$("#newPurview").hide();
		$("#newMenu").hide();
		$("#pruview_list_table").hide();
		$("#menu_list_table").hide();
		if(treeNode.children!=null&&treeNode.children.length>0){
			//存在子菜单 可以新增菜单
			$("#newMenu").show();
			$scope.menuQueryForm.parentid=treeNode.sysMenuId;
			queryMenu($scope.menuQueryForm);
			$("#menu_list_table").show();
		}else{
			//不存在子菜单
			//判断是否是顶级菜单如果是则只可以新增菜单
			if(treeNode.parentid==null){
				$("#newMenu").show();
			}else{
			//如果不是 发送请求查询权限 存在权限则只可以新增权限  如果不存在显示新增菜单和新增权限
				$scope.purQueryForm.parentid=treeNode.sysMenuId;
				var url=commonutil.actionPath + '/sys/menu-purview/list';
				anajax.doajax(url, $scope.purQueryForm, function(data) {
					if(data.resultList.length>0){
						$("#newPurview").show();
						$("#pruview_list_table").show();
					$scope.pruviewResultList = data.resultList;
					pagination.setData(data.pagination);
					$scope.pagination = data.pagination; 
					}else{
						$("#newPurview").show();
						$("#newMenu").show();
					}
				});
			}
		}
		}
	

	
	
	
	
	/**初始化树*/
	var init = function() {
		var url = commonutil.actionPath + '/sys/menu/menu_list';
		anajax.doajax(url, null, function(data) {
			var zTree =  $.fn.zTree.init($("#sysMenuTree"), $scope.setting, data);
			zTree.expandAll(true);
			 var treeObj = $.fn.zTree.getZTreeObj("sysMenuTree");
			var menuid=$scope.menuQueryForm.parentid;
			if(menuid!=null){
			 var sNodes = treeObj.getNodesByParam("sysMenuId",menuid , null);
			 if (sNodes.length > 0) {
				 var  nodes=sNodes[0];
				 $("#"+nodes.tId+"_a").click();
			 } 
			}
			
		}); 
		
	};
	
	init();
	
	
	
	
	
	/**初始化新增菜单*/
	$scope.goAddMenu = function(index) {
		$scope.menuForm = {
		};
		$scope.menuForm.dadddate = null;
		$scope.menuForm.dlastupdatetime = null;
		$scope.menuForm.dcreatetime = null;
		$scope.stations = [];
		$scope.rightPart = 'add';
		removeTips($('#edit-menu-data-warp'));
		$('#edit-purview-data-warp').hide();
		$('#edit-menu-data-warp').show();
		pageutil.showRightBarAn('新增菜单');
	};
	
	
	/**初始化编辑菜单*/
	$scope.goModifyMenu = function(index) {
		$scope.rightPart = 'edit';
		var assets = $scope.menuResultList[index];
		angular.copy(assets, $scope.menuForm);
		$scope.menuForm.dadddate = null;
		$scope.menuForm.dlastupdatetime = null;
		$scope.menuForm.dcreatetime = null;
		removeTips($('#edit-menu-data-warp'));
		$('#edit-purview-data-warp').hide();
		$('#edit-menu-data-warp').show();
		pageutil.showRightBarAn('编辑菜单信息');
	};
	
	
	/**初始化新增权限*/
	$scope.goAddPurview = function(index) {
		$scope.purForm = {
		};
		$scope.stations = [];
		$scope.rightPart = 'add';
		removeTips($('#edit-menu-data-warp'));
		$('#edit-purview-data-warp').show();
		$('#edit-menu-data-warp').hide();
		pageutil.showRightBarAn('新增权限');
	};
	
	
	/**初始化编辑权限*/
	$scope.goModifyPurview = function(index) {
		$scope.rightPart = 'edit';
		var assets = $scope.pruviewResultList[index];
		angular.copy(assets, $scope.purForm);
		$scope.purForm.dadddate = null;
		$scope.purForm.dlastupdatetime = null;
		$scope.purForm.dcreatetime = null;
		removeTips($('#edit-menu-data-warp'));
		$('#edit-purview-data-warp').show();
		$('#edit-menu-data-warp').hide();
		pageutil.showRightBarAn('编辑权限信息');
	};
	
	
	$scope.doAddMenu=function(){
		$scope.menuForm.parentid=$scope.menuQueryForm.parentid;
		var res = Validator.validate($("#edit-menu-data-warp"));
		if(!res){
			return;
		}
		if($scope.rightPart=='add'){
			$scope.addMenu($scope.menuForm);
		}else{
			$scope.modifyMenu($scope.menuForm);
		}
		
	}
	
	$scope.addMenu=function(postData){
		var url = commonutil.actionPath + '/sys/menu/add';
		anajax.doajax(url, postData, function(data) {
			if (data.resultValue ) {
				pageutil.showTip('添加成功');
				pageutil.hideRightBar();
				$("#newPurview").hide();
				queryMenu($scope.menuQueryForm);
				init();
			} else {
				pageutil.showTip(data.message);
			}
		});
		
	}
	
	$scope.modifyMenu=function(postData){
		var url = commonutil.actionPath + '/sys/menu/update';
		anajax.doajax(url, postData, function(data) {
			if (data.resultValue ) {
				pageutil.showTip('修改成功');
				pageutil.hideRightBar();
				queryMenu($scope.menuQueryForm);
			} else {
				pageutil.showTip(data.message);
			}
		});
		
	}
	
	
	$scope.doAddPurview=function(){
		$scope.purForm.parentid=$scope.purQueryForm.parentid;
		var res = Validator.validate($("#edit-purview-data-warp"));
		if(!res){
			return;
		}
		if($scope.rightPart=='add'){
			$scope.addPurview($scope.purForm);
		}else{
			$scope.modifyPurview($scope.purForm);
		}
		
	}
	
	
	
	$scope.addPurview=function(postData){
		var url = commonutil.actionPath + '/sys/menu-purview/add';
		anajax.doajax(url, postData, function(data) {
			if (data.resultValue ) {
				pageutil.showTip('添加成功');
				pageutil.hideRightBar();
				$("#newMenu").hide();
				queryPurview($scope.purQueryForm);
			} else {
				pageutil.showTip(data.message);
			}
		});
	}
	
	$scope.modifyPurview=function(postData){
		var url = commonutil.actionPath + '/sys/menu-purview/update';
		anajax.doajax(url, postData, function(data) {
			if (data.resultValue ) {
				pageutil.showTip('添加成功');
				pageutil.hideRightBar();
				queryPurview($scope.purQueryForm);
			} else {
				pageutil.showTip(data.message);
			}
		});
	}
	
	
	$scope.doCancel = function(){
		pageutil.hideRightBar();
	}
	
	$scope.delMenuBean ={};
	$scope.delPurviewBean ={};
	
	/**
	 * 弹出确认删除窗口
	 */
	$scope.showDelWinMenu = function($index){
		var assetBean = $scope.menuResultList[$index];
		$scope.delMenuBean= assetBean;
		$("#confirm_del_menu_asset_div").modal('show');
	};
	
	
	/**
	 * 弹出确认删除窗口
	 */
	$scope.showDelWinPurview = function($index){
		var assetBean = $scope.pruviewResultList[$index];
		$scope.delPurviewBean= assetBean;
		$("#confirm_del_purview_asset_div").modal('show');
	};
	
	//删除菜单
	$scope.doDelMenuAsset = function() {
	    console.log($scope.delMenuBean);
		var url = commonutil.actionPath + '/sys/menu/del';
		anajax.doajax(url,{id:$scope.delMenuBean.sysMenuId}, function(data) {
			if (data.resultValue ) {
				pageutil.showTip('删除成功');
				queryMenu($scope.menuQueryForm);
				init();
			} else {
				pageutil.showTip(data.message);
			}
		});
	};
	
	//删除权限
	$scope.doDelPurviewAsset = function() {
	    console.log($scope.delPurviewBean);
		var url = commonutil.actionPath + '/sys/menu-purview/del';
		anajax.doajax(url,{id:$scope.delPurviewBean.sysPurviewId}, function(data) {
			if (data.resultValue ) {
				pageutil.showTip('删除成功');
				queryPurview($scope.purQueryForm);
			} else {
				pageutil.showTip(data.message);
			}
		});
	};
	
	
	
});